﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class PreContractInfoComment: IPreContractInfoComment
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public PreContractInfoComment(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.PreContractInfoComment GetPreContractInfoComment(System.Int32 preContractInfoCommentID)
		{
			BEGDC.PreContract.DAL.PreContractInfoComment preContractInfoComment = BEGDC.PreContract.DAL.PreContractInfoComment.TryFind(preContractInfoCommentID);

            return preContractInfoComment;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetPreContractInfoComment_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM PreContractInfoComment";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeletePreContractInfoComment(System.Int32 preContractInfoCommentID)
		{
			try
            {
				BEGDC.PreContract.DAL.PreContractInfoComment preContractInfoComment = BEGDC.PreContract.DAL.PreContractInfoComment.TryFind(preContractInfoCommentID);
                preContractInfoComment.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("PreContractInfoComment", string.Format("Error occured when deleting the PreContractInfoComment, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddPreContractInfoComment(BEGDC.PreContract.DAL.PreContractInfo preContractID, System.String comment, BEGDC.PreContract.DAL.Employee creator, Nullables.NullableDateTime dateCreated, BEGDC.PreContract.DAL.Employee modifiedBy, Nullables.NullableDateTime dateModified, System.Boolean isRisk, Nullables.NullableDateTime commentPromisedDate)	
		{
			try
            {
				BEGDC.PreContract.DAL.PreContractInfoComment preContractInfoComment = new BEGDC.PreContract.DAL.PreContractInfoComment();
				
				preContractInfoComment.PreContractID = preContractID;
				preContractInfoComment.Comment = comment;
				preContractInfoComment.Creator = creator;
				preContractInfoComment.DateCreated = dateCreated;
				preContractInfoComment.ModifiedBy = modifiedBy;
				preContractInfoComment.DateModified = dateModified;
				preContractInfoComment.IsRisk = isRisk;
				preContractInfoComment.CommentPromisedDate = commentPromisedDate;
				preContractInfoComment.CreateAndFlush();
				
				return preContractInfoComment.PreContractInfoCommentID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("PreContractInfoComment", string.Format("Error occured when adding a new PreContractInfoComment, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdatePreContractInfoComment(System.Int32 preContractInfoCommentID, BEGDC.PreContract.DAL.PreContractInfo preContractID, System.String comment, BEGDC.PreContract.DAL.Employee creator, Nullables.NullableDateTime dateCreated, BEGDC.PreContract.DAL.Employee modifiedBy, Nullables.NullableDateTime dateModified, System.Boolean isRisk, Nullables.NullableDateTime commentPromisedDate)
		{
			try
            {
                BEGDC.PreContract.DAL.PreContractInfoComment preContractInfoComment = GetPreContractInfoComment(preContractInfoCommentID);

				preContractInfoComment.PreContractID = preContractID;
				preContractInfoComment.Comment = comment;
				preContractInfoComment.Creator = creator;
				preContractInfoComment.DateCreated = dateCreated;
				preContractInfoComment.ModifiedBy = modifiedBy;
				preContractInfoComment.DateModified = dateModified;
				preContractInfoComment.IsRisk = isRisk;
				preContractInfoComment.CommentPromisedDate = commentPromisedDate;
				preContractInfoComment.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("PreContractInfoComment", string.Format("Error occured when updating the PreContractInfoComment, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



